package com.hwhhhh.mygrad.fragment;

import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.reflect.TypeToken;
import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.adapter.AdminClsAdapter;
import com.hwhhhh.mygrad.api.Api;
import com.hwhhhh.mygrad.api.ApiCallback;
import com.hwhhhh.mygrad.api.ApiConfig;
import com.hwhhhh.mygrad.dto.FreeClsDTO;
import com.hwhhhh.mygrad.entity.RespDataArr;
import com.hwhhhh.mygrad.utils.StringUtils;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class ClsManageFragment extends BaseFragment{
    private RecyclerView rvClsList;
    private TextView tvDate;
    private List<FreeClsDTO> freeClsDTOList;
    private AdminClsAdapter adminClsAdapter;

    @Override
    protected int initLayout() {
        return R.layout.fragment_cls_manage;
    }

    @Override
    protected void initView() {
        rvClsList = mRootView.findViewById(R.id.rv_cls_list);
        tvDate = mRootView.findViewById(R.id.tv_date);
    }

    @Override
    protected void initData() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String str = year + "-" + month + "-" + day;
        tvDate.setText(str);
        requestData();
    }

    private void requestData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("freeDate", tvDate.getText().toString());
        params.put("campus", 2);
        params.put("start", 1);
        params.put("end", 13);
        Api.config(ApiConfig.FREE_CLSLIST, params).getRequest(getActivity(), new ApiCallback() {
            @Override
            public void onSuccess(String res) {
                RespDataArr<FreeClsDTO> dataArr = StringUtils.fromJsonArr(res, new TypeToken<RespDataArr<FreeClsDTO>>(){}.getType());
                if (dataArr.getCode() == 200) {
                    freeClsDTOList = dataArr.getData();
                    getActivity().runOnUiThread(() -> {
                        adminClsAdapter = new AdminClsAdapter(getActivity(), freeClsDTOList);
                        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
                        rvClsList.setLayoutManager(layoutManager);
                        rvClsList.setAdapter(adminClsAdapter);
                    });
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    public AdminClsAdapter getAdminClsAdapter() {
        return this.adminClsAdapter;
    }
}
